const axios = require("axios")

const ClientID = "0d54865545c82d618625";
const ClientSecret = "f55f24ed9ed78647711b9ed4b5271e31d9173300";

module.exports = async (req, res, next) => {
    const reqToken = req.query.code;
    // console.log("auth code:::::", reqToken);

    const tokenRes = await axios({
        method: "post",
        url: `https://github.com/login/oauth/access_token?client_id=${ClientID}&client_secret=${ClientSecret}&code=${reqToken}`,
        headers: {
            accept: "application/json"
        }
    });
    // console.log("tokenResData:::::", tokenRes)

    const accessToken = tokenRes.data.access_token;
    // console.log("accessToken:::::", accessToken)

    const result = await axios({
        method: "get",
        url: "https://api.github.com/user",
        headers: {
            accept: "application/json",
            Authorization: `token ${accessToken}`
        }
    });

    console.log("resultData::::::", result.data);

    res.redirect(`http://localhost:8081`, {
        userInfo: result.data
    });

    next()
}